/*************************************************************************
 * Copyright 2008 David Almilli
 *  
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0 
 *      
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 **************************************************************************/
package com.tivo.hme.bananas;

import com.tivo.hme.interfaces.IContext;
import com.tivo.hme.sdk.HmeEvent;
import com.tivo.hme.sdk.Resolution;
import com.tivo.hme.sdk.HmeEvent.ResolutionInfo;

/**
 * The <code>BApplicationPlus</code> class extends BApplication to automatically change to the
 * preferred rendering resolution.  It also configures the application skin to use the appropriate
 * skin for that resolution.  After the resolution change happens, it will call the init() method.
 * @author s2kdave
 */
public class BApplicationPlus extends BApplication {
    private Resolution desiredResolution;

    private boolean initCalled;

    @Override
    public void init(IContext context) throws Exception {
        super.init(context);
        setSkin(new BSkinPlus(this));

        ResolutionInfo resInfo = getReceiverResolutionInfo();
        Resolution curRes = resInfo.getRenderingResolution();
        desiredResolution = getPreferredResolution(resInfo);
        if (curRes.equals(desiredResolution)) {
            initCalled = true;
            initSkin();
            init();
        } else {
            setReceiverResolution(desiredResolution);
        }
    }

    /**
     * Selects the preferred resolution for this application given the list of supported
     * resolutions. You may override this if you want to select a different resolution other than
     * calling resInfo.getPreferredRenderingResolution().
     * 
     * @param resInfo
     *            contains all the supported resolutions.
     * @return the desired resolution.
     */
    protected Resolution getPreferredResolution(ResolutionInfo resInfo) {
        return resInfo.getPreferredRenderingResolution();
    }

    public int getSafeActionHorizontal() {
        return getCurrentResolution().getSafeActionHorizontal();
    }

    public int getSafeActionVertical() {
        return getCurrentResolution().getSafeActionVertical();
    }

    public int getSafeTitleHorizontal() {
        return getCurrentResolution().getSafeTitleHorizontal();
    }

    public int getSafeTitleVertical() {
        return getCurrentResolution().getSafeTitleVertical();
    }

    public BSkinPlus getSkin() {
        return (BSkinPlus) super.getSkin();
    }

    @Override
    public void setSkin(BSkin skin) {
        if (!(skin instanceof BSkinPlus)) {
            throw new IllegalArgumentException("You must use a BSkinPlus with this application.");
        }
        super.setSkin(skin);
    }

    @Override
    public boolean handleEvent(HmeEvent event) {
        if (event instanceof ResolutionInfo) {
            ResolutionInfo resInfo = (ResolutionInfo) event;
            if (!initCalled && desiredResolution != null
                    && resInfo.getRenderingResolution().equals(desiredResolution)) {
                initCalled = true;
                initSkin();
                init();
            }
        }
        return super.handleEvent(event);
    }

    protected void initSkin() {
        getSkin().configure(getCurrentResolution());
    }

    /**
     * This init method gets called after the resolution has been changed to the 
     * preferred resolution.
     */
    protected void init() {
    }

}
